package svc

import (
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"gorm.io/gorm/schema"
	"gozero/internal/config"
	"gozero/internal/models"
)

type ServiceContext struct {
	Config  config.Config
	DbEngin *gorm.DB
}

func NewServiceContext(c config.Config) *ServiceContext {

	db, err := gorm.Open(mysql.Open(c.DataSourceName), &gorm.Config{
		NamingStrategy: schema.NamingStrategy{
			TablePrefix:   "tech_",
			SingularTable: true, //表名如果是单数 启用该选项
		},
	})
	if err != nil {
		panic(err)
	}
	db.AutoMigrate(&models.User{})
	return &ServiceContext{
		Config:  c,
		DbEngin: db,
	}
}
